// Vue
import Vue from 'vue'
import i18n from './i18n'
import App from './App'
// 核心插件
import d2Admin from '@/plugin/d2admin'
// store
import store from '@/store/index'
// [ 可选插件 ] 数据导入导出
import pluginExport from '@d2-projects/vue-table-export'
import pluginImport from '@d2-projects/vue-table-import'

// [ 可选组件 ]D2-Crud
import D2Crud from '@d2-projects/d2-crud'
// [ 可选组件 ] 图表
// import VCharts from 'v-charts'

// 菜单和路由设置
import router from './router'
// 暂时如此获取baseURL
import util from '@/libs/util.js'
import { reqMyUrl } from '@api/system.api.fetch'
reqMyUrl().then(async r => {
  util.cookies.set('myUrl', r.data)
})
// 核心插件
Vue.use(d2Admin, { store })
Vue.use(D2Crud)
Vue.use(pluginExport)
Vue.use(pluginImport)

new Vue({
  router,
  store,
  i18n,
  render: h => h(App),
  created () {
    // 菜单相关内容转到router/index.js内
  },
  mounted () {
    // 展示系统信息
    this.$store.commit('d2admin/releases/versionShow')
    // 用户登录后从数据库加载一系列的设置
    this.$store.dispatch('d2admin/account/load')
    // 获取并记录用户 UA
    this.$store.commit('d2admin/ua/get')
    // 初始化全屏监听
    this.$store.dispatch('d2admin/fullscreen/listen')
  },
  watch: {
    // 检测路由变化切换侧边栏内容
    '$route.matched': {
      handler (matched) {
        let fullAside = this.$store.state.d2admin.menu.fullAside
        if (matched.length > 0) {
          const _side = fullAside.filter(menu => menu.path === matched[0].path)
          this.$store.commit('d2admin/menu/asideSet', _side.length > 0 ? _side[0].children : [])
        }
      },
      immediate: true
    }
  }
}).$mount('#app')
